package com.dc.project.system.controller;

import com.dc.common.validated.InsertGroup;
import com.dc.common.validated.QueryPageGroup;
import com.dc.common.validated.UpdateGroup;
import com.dc.common.vo.ResultVo;
import com.dc.project.system.dto.SysRoleDto;
import com.dc.project.system.dto.SysUserRoleDto;
import com.dc.project.system.entity.SysRole;
import com.dc.project.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
 * 系统角色表(SysRole)表控制层
 *
 * @author zhuangcy
 * @since 2021-06-01 14:31:08
 */
@RestController
@RequestMapping("sysRole")
@Api(tags = "系统角色表")
public class SysRoleController {
    /**
     * 服务对象
     */
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("通过主键查询单条数据")
    @RequiresPermissions("system:sysRole:query")
    public ResultVo get(@PathVariable Serializable id) {
        return this.sysRoleService.queryById(id);
    }

    /**
     * 分页查询系统角色表
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("分页查询系统角色表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", required = true)
    })
    @RequiresPermissions("system:sysRole:query")
    public ResultVo listPage(@Validated(QueryPageGroup.class) SysRoleDto dto) {
        return this.sysRoleService.queryByPage(dto);
    }

    /**
     * 新增系统角色表
     *
     * @param sysRole
     * @return
     */
    @PostMapping
    @ApiOperation("新增系统角色表")
    @RequiresPermissions("system:sysRole:add")
    public ResultVo add(@Validated(value = {InsertGroup.class}) @RequestBody SysRole sysRole) {
        return this.sysRoleService.add(sysRole);
    }

    /**
     * 编辑系统角色表
     *
     * @param sysRole
     * @return
     */
    @PutMapping
    @ApiOperation("编辑系统角色表")
    @RequiresPermissions("system:sysRole:edit")
    public ResultVo edit(@Validated(value = {UpdateGroup.class}) @RequestBody SysRole sysRole) {
        return this.sysRoleService.edit(sysRole);
    }

    /**
     * 根据主键删除系统角色表
     *
     * @param id 主键
     * @return
     */
    @DeleteMapping("{id}")
    @ApiOperation("根据主键删除系统角色表")
    @RequiresPermissions("system:sysRole:delete")
    public ResultVo delete(@PathVariable Integer id) {
        return this.sysRoleService.delete(id);
    }

    /**
     * 根据用户id获取用户角色列表
     *
     * @param userId 用户id
     * @return
     */
    @GetMapping("userRole/{userId}")
    @ApiOperation("根据用户id获取用户角色列表")
    @RequiresPermissions("system:sysRole:query")
    public ResultVo getUserRole(@PathVariable Integer userId) {
        return this.sysRoleService.getUserRole(userId);
    }

    /**
     * 分页查询根据用户获取用户角色列表
     *
     * @param dto
     * @return
     */
    @GetMapping("userRolePage")
    @ApiOperation("分页查询根据用户获取用户角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", required = true)
    })
    @RequiresPermissions("system:sysRole:query")
    public ResultVo getUserRolePage(@Validated(QueryPageGroup.class) SysRoleDto dto) {
        return this.sysRoleService.getUserRolePage(dto);
    }


    /**
     * 编辑系统角色权限
     *
     * @param sysRole
     * @return
     */
    @PutMapping("menuDept")
    @ApiOperation("编辑系统角色权限")
    @RequiresPermissions("system:sysRole:menu")
    public ResultVo editRoleMenuDept(@Validated(value = {InsertGroup.class}) @RequestBody SysRoleDto sysRole) {
        return this.sysRoleService.editRoleMenuDept(sysRole);
    }


    /**
     * 获取用户角色
     *
     * @param dto
     * @return
     */
    @GetMapping("userRole")
    @ApiOperation("获取用户角色")
    @RequiresPermissions(value = {"system:sysRole:query", "system:sysUser:add", "system:sysUser:edit"}, logical = Logical.OR)
    public ResultVo getUserRoleList(SysUserRoleDto dto) {
        return this.sysRoleService.getUserRoleList(dto);
    }

}